#include <glad/glad.h> //需要在最前面  //GLAD是用来管理OpenGL的函数指针的
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include "Shader.h"
#include "Geo.h"
#include "Texture.h"

#include "GuiFrame.h"


int main()
{
	//创建窗口
	GLFWwindow* pWindow=GuiFrame::Create_Glfw_Window();
	if(pWindow== nullptr)
	{
		glfwTerminate();
		return -1;
	}

	GuiFrame::Init_ImGui(pWindow);

	//渲染 循环
	Shader shader("../shader/vertexShader.glsl","../shader/fragmentShader.glsl");
	Texture texture0("../assert/texture/container.jpg",GL_RGB);
	Texture texture1("../assert/texture/awesomeface.png",GL_RGBA);

	GeoBox geo(1,1,1,100,100,100);
	glm::mat4 trans(1.0f);
	glm::mat4 projection = glm::perspective(glm::radians(GuiFrame::Windows_Control::getController()->camera.Zoom), (float)WIN_WIDTH / (float)WIN_HEIGHT, 0.1f, 100.0f);

	while(!glfwWindowShouldClose(pWindow))
	{
		//timing计算
		float currentFrame = static_cast<float>(glfwGetTime());
		GuiFrame::Windows_Control::getController()->deltaTime = currentFrame - GuiFrame::Windows_Control::getController()->lastFrame;
		GuiFrame::Windows_Control::getController()->lastFrame = currentFrame;

		//捕获输入
		GuiFrame::Windows_Control::getController()->processInput(pWindow);
		GuiFrame::Render_ImGui();

		//绘制
		glClearColor(0.2,0.3,0.3,1.0);
		glClear(GL_COLOR_BUFFER_BIT);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		shader.use();

		glBindVertexArray(geo.vao());
		glActiveTexture(GL_TEXTURE0);//纹理
		glBindTexture(GL_TEXTURE_2D,texture0.getID());
		glActiveTexture(GL_TEXTURE1);
		glBindTexture(GL_TEXTURE_2D,texture1.getID());
		shader.setInt("ourTexture0",0);
		shader.setInt("ourTexture1",1);

		// view/projection transformations

		glm::mat4 view = GuiFrame::Windows_Control::getController()->camera.GetViewMatrix();
		shader.setVec3("viewPos",GuiFrame::Windows_Control::getController()->camera.Position);
		shader.setMat4("projection", projection);
		shader.setMat4("view", view);

		glm::mat4 model(1);
		model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));

		shader.setMat4("model",model);
		shader.setMat4("transform",trans);


		glDrawElements(GL_LINE_LOOP,geo.vertexCount(),GL_UNSIGNED_INT, nullptr);

		glfwSwapBuffers(pWindow);
		glfwPollEvents();
	}

	glfwTerminate();
	GuiFrame::Windows_Control::Controller_free();
	return 0;
}